package com.wgl.yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wgl.model.hosp.HospitalSet;
import com.wgl.model.hosp.HospitalSetQueryVo;
import com.wgl.yygh.common.util.MD5;
import com.wgl.yygh.result.Result;
import com.wgl.yygh.serivce.HospitalSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName HospitalSetController.java
 * @Description TODO
 * @createTime 2022年02月03日 14:52:00
 */
//@CrossOrigin
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Resource
    private HospitalSetService hospitalSetService;
    private Random random =new Random();

    @ApiOperation(value = "获得所有的医院")
    //localhost:8201/admin/hosp/hospitalSet/findAll
    @GetMapping("/findAll")
    public Result findAllHospitalSet(){

        return Result.ok(hospitalSetService.list());
    }
    @ApiOperation(value = "根据id删除医院--逻辑删除")
    @DeleteMapping("/deleteHosp/{id}")
    public Result removeHospSet(@PathVariable Long id){
        return Result.ok(hospitalSetService.removeById(id));
    }
    //条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current, @PathVariable Long limit , @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建分页对象
        Page<HospitalSet> page = new Page<>(current,limit);
        System.out.println(hospitalSetQueryVo);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if(hospitalSetQueryVo != null){

            if (!StringUtils.isEmpty(hospitalSetQueryVo.getHospname())) {
                queryWrapper.like("hosname",hospitalSetQueryVo.getHospname());

            }
            if (!StringUtils.isEmpty(hospitalSetQueryVo.getHospcode())) {
                queryWrapper.eq("hoscode",hospitalSetQueryVo.getHospcode());

            }
        }



        return Result.ok(hospitalSetService.page(page,queryWrapper));
    }
    //添加医院
    @ApiOperation(value = "添加医院")
    @PostMapping("/saveHospSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet){
        //设置状态
        hospitalSet.setStatus(1);
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        return save?Result.ok():Result.fail();
    }
    // 根据id查询医院设置
    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        return Result.ok(hospitalSetService.getById(id));
    }
    // 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        return hospitalSetService.updateById(hospitalSet)?Result.ok():Result.fail();
    }
    // 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Integer> list){
        return hospitalSetService.removeByIds(list)?Result.ok():Result.fail();
    }
    // 医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        //先查
        HospitalSet byId = hospitalSetService.getById(id);
        byId.setStatus(status);

        return hospitalSetService.updateById(byId)?Result.ok():Result.fail();
    }
    // 发送医院对接的密钥
    @ApiOperation(value = "发送医院对接的密钥")
    @DeleteMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet byId = hospitalSetService.getById(id);
        //TODO  发送短信

        return Result.ok();
    }

}
